window.onload=function(){
    getsrcLen();
    if(sessionStorage['token']){
        unsubmit(true);
        console.log(searchInfo);
    }
}
/*$("[data-target='#src-more']").click(function () {
	alert(1);

})*/
var clickNum = 0;
var searchInfo = {};
var isChange = false;
$(".cancel").click(function () {
    $("#changePasswordWindow").css("display", "none");
})
$("#closeButton").click(function () {
    $("#changePasswordWindow").css("display", "none");
})
$("#changePassword").click(function () {
    $("#changePasswordWindow").css("display", "");
});
$(".changeButton").click(
    // function(){
    //     var srctext=$("#src-button").find('button').html();
    //     var srcId=$("#src-button").find('button').attr("id");
    //     var srcName=$("#src-button").find('button').attr("name");
    //     var dsttext=$("#dst-button").find('button').html();
    //     var dstId=$("#dst-button").find('button').attr("id");
    //     var dstName=$("#dst-button").find('button').attr("name");
    //     $("#src-button").find('button').html(dsttext);
    //     $("#dst-button").find('button').html(srctext);
    //     $("#src-button").find('button').attr("id",dstId);
    //     $("#dst-button").find('button').attr("id",srcId);
    //     $("#src-button").find('button').attr("name",dstName);
    //     $("#dst-button").find('button').attr("name",srcName);
    //     $('#dest-more').find('a').css("cursor",'pointer');
    //     $('#dest-more').find('a').css("color","#000");
    //     $('#dest-more').find('a').click(function(){changeArea(this,10)});
    //     $('#src-more').find('a').css("cursor",'pointer');
    //     $('#src-more').find('a').css("color","#000");
    //     $('#src-more').find('a').click(function(){changedstLan(this,true)});
    //     var newsrcName=$("#src-button").find('button').attr("id");
    //     var newdstName=$("#dst-button").find('button').attr("id");
    //     $('#dest-more').find('a[name='+newsrcName+']').css("cursor","not-allowed");
    //     $('#dest-more').find('a[name='+newsrcName+']').css("color","#e5e5e5");
    //     $('#dest-more').find('a[name='+newsrcName+']').attr("disabled",true);
    //     $('#dest-more').find('a[name='+newsrcName+']').removeAttr("onclick");
    //     $('#dest-more').find('a[name='+newsrcName+']').unbind();
    //     $('#src-more').find('a[name='+newdstName+']').css("cursor","not-allowed");
    //     $('#src-more').find('a[name='+newdstName+']').css("color","#e5e5e5");
    //     $('#src-more').find('a[name='+newdstName+']').attr("disabled",true);
    //     $('#src-more').find('a[name='+newdstName+']').removeAttr("onclick");
    //     $('#src-more').find('a[name='+newdstName+']').unbind();
    //
    // }
);

$(".select_box input").click(function () {
    $("#selectbutton").click();
});

setInterval(function(){
    var a= $("#body-row").html();
    /*   console.log(a);*/
    $("#Orderbtn").attr("data-target","#");
    $("#Orderbtn").removeClass();
    $("#Orderbtn").addClass('agreebutton');
    if($("#size").html()=="计算中..."){
        $("#Orderbtn").attr("data-target","#change");
    }
    console.log(length);
    if($("#body-row").html() == ""){
        clickNum = 0;
    }
    if(searchInfo.srcLang && $("#body-row").html() !== ""){
        clickNum = 1;
        console.log(clickNum);
    }
    if($('#select1').children().html()=='请选择'||$('#select2').children().html()=='请选择'||$("#body-row").html()==""||$("#size").html()=="计算中..."){

        $("#Orderbtn").attr('disabled',false);
        $("#Orderbtn").removeClass();
        $("#Orderbtn").addClass('graybutton');
    }else{
        $("#Orderbtn").removeAttr('disabled');
    }

},1000);
$("#Orderbtn").click(function () {
    if($("#Orderbtn").attr("data-target")=='#myModal'){
        setTimeout(function(){
            $("#myModal").modal('hide');
        },3000);}
    if($("#Orderbtn").attr("data-target")=='#change'){
        setTimeout(function(){
            $("#change").modal('hide');
        },3000);
    }

});
var dropbox = document.getElementById("dropbox");
var dropSpan = document.getElementById("drop-span");
// 点击选择
$("#completeButton").click(function () {
    if($('#src-button1>button').html()=='请选择'||$('#src-button1>button').html()=='请选择'){
        alert("请选择语言信息")
    } else {
        $('#languageModal').modal('hide');
        var files = dropbox.files;
        for(var i = 0;i < files.length;i++){
            var file = files[i];
            isChange = false
            if(file.name.endsWith('.docx')||file.name.endsWith('.pptx')||file.name.endsWith('.xlsx')||file.name.endsWith('.XLSX')||file.name.endsWith('.DOCX')||file.name.endsWith('.PPTX')){
                dynamicInfo(file,true);
            }else if(file.name.endsWith('.png')||file.name.endsWith('.jpg')||file.name.endsWith('.pdf')||file.name.endsWith('.PNG')||file.name.endsWith('.JPG')||file.name.endsWith('.PDF')){
                isChange = true;
                dynamicInfo(file,true);
            }
            else{
                alert('仅支持docx,png,jpg,pdf,pptx,xlsx格式文件上传！');
            }
        }
        $("#dropbox").val('');
    }
});
//点击选择
dropbox.addEventListener('change', function(event) {
    console.log("clickNum=" + clickNum);
    if(clickNum == 0){
        $('#languageModal').modal('show');
    } else {
        var files = dropbox.files;
        for(var i = 0;i < files.length;i++){
            var file = files[i];
            isChange = false
            if(file.name.endsWith('.docx')||file.name.endsWith('.pptx')||file.name.endsWith('.xlsx')||file.name.endsWith('.XLSX')||file.name.endsWith('.DOCX')||file.name.endsWith('.PPTX')){
                dynamicInfo(file,true);
            }else if(file.name.endsWith('.png')||file.name.endsWith('.jpg')||file.name.endsWith('.pdf')||file.name.endsWith('.PNG')||file.name.endsWith('.JPG')||file.name.endsWith('.PDF')){
                isChange = true;
                dynamicInfo(file,true);
            }
            else{
                alert('仅支持docx,png,jpg,pdf,pptx,xlsx格式文件上传！');
            }
        }
        $("#dropbox").val('');
    }
    $("#completeButton").show();
    $("#dropCompleteButton").hide();

}, false);
// 拖拽选择
dropSpan.addEventListener("dragenter", function(e){
    e.stopPropagation();
    e.preventDefault();
}, false);

dropSpan.addEventListener("dragover", function(e){
    e.stopPropagation();
    e.preventDefault();
}, false);
var dt ={};
$("#dropCompleteButton").click(function () {
    if($('#select1').children().html()=='请选择'||$('#select2').children().html()=='请选择'){
        alert("请选择语言信息")
    } else {
        $('#languageModal').modal('hide');
        for (let i = 0; i < dt.length; i++) {
            isChange = false;
            var file = dt[i];
            if(file.name.endsWith('.docx')||file.name.endsWith('.pptx')||file.name.endsWith('.xlsx')||file.name.endsWith('.XLSX')||file.name.endsWith('.DOCX')||file.name.endsWith('.PPTX')){
                dynamicInfo(file,true);
            }else if(file.name.endsWith('.png')||file.name.endsWith('.jpg')||file.name.endsWith('.pdf')||file.name.endsWith('.PNG')||file.name.endsWith('.JPG')||file.name.endsWith('.PDF')){
                isChange = true;
                dynamicInfo(file, true);
            }else{
                alert('仅支持docx,png,jpg,pdf,pptx,xlsx格式文件上传！');
            }
        }
        $("#dropbox").val('');
    }
});
dropSpan.addEventListener("drop", function(e){
    e.stopPropagation();
    e.preventDefault();
    dt = e.dataTransfer.files;
    $("#completeButton").hide();
    $("#dropCompleteButton").show();
    if(clickNum == 0) {
        $('#languageModal').modal('show');
    } else {
        for (let i = 0; i < dt.length; i++) {
            isChange = false;
            var file = dt[i];
            if(file.name.endsWith('.docx')||file.name.endsWith('.pptx')||file.name.endsWith('.xlsx')||file.name.endsWith('.XLSX')||file.name.endsWith('.DOCX')||file.name.endsWith('.PPTX')){
                dynamicInfo(file,true);
            }else if(file.name.endsWith('.png')||file.name.endsWith('.jpg')||file.name.endsWith('.pdf')||file.name.endsWith('.PNG')||file.name.endsWith('.JPG')||file.name.endsWith('.PDF')){
                isChange = true;
                dynamicInfo(file, true);
            }else{
                alert('仅支持docx,png,jpg,pdf,pptx,xlsx格式文件上传！');
            }
        }
        $("#dropbox").val('');
    }
    console.log("滑动");
}, false);
//文档动态上传
function dynamicInfo(data,key){
    clickNum = clickNum + 1;
    if(sessionStorage['token']){
        var wait=document.getElementById("wait");
        wait.value=JSON.stringify({"userId":sessionStorage['userId'], "filesize":data.size});
        var formData=document.getElementById("formData");
        formData=new FormData(formData);
        var srcId = $('#src-button > .border_select').attr('id');
        var tgtId =  $("#dst-button1 > button").eq(0).attr('dataId');
        if(key){
            //拖拽上传，set文件
            formData.set('file', data, data.name);
            formData.set('srcLangId', srcId);
            formData.set('tgtLangId', tgtId);
        }
        var index=$("#body-row >div").length+1;
        var progressId = "progress"+ index;
        var html='<div class="content-file-row">'+
            '<div class="col-md-1 col-xs-2">'+index+'</div>'+
            '<div class="col-md-5 col-xs-4">'+data.name+'</div>'+
            '<div class="col-md-2 col-xs-2">-</div>'+
            '<div class="col-md-3 col-xs-3">'+
            '<div class="progress">'+
            '<div class="progress-bar" role="progressbar" aria-valuenow="100" id= "' + progressId + '" aria-valuemin="0" aria-valuemax="100" style="width:0%;">'+
            '</div>'+
            '</div>'+
            '</div>'+
            /*'<div class="col-md-1 col-xs-1">'+
                '<a class="glyphicon glyphicon-trash delete-btn" aria-hidden="true" onclick="deleteinfo(this)" ></a> '+
            '</div>'+*/
            '</div>';
        $('#body-row').append(html);
        $('#body-row button').each(function(){
            $(this).attr('disabled','disabled');
            $(this).css('curson','not-allowed')
        });
        $.ajax({
            url: URL_ip+"/addWait",
            type: "POST",
            data: formData,
            beforeSend: function(request) {
                request.setRequestHeader("X-OAUTH-TOKEN", sessionStorage['token']);
            },
            xhr: function(){ //获取ajaxSettings中的xhr对象，为它的upload属性绑定progress事件的处理函数
                myXhr = $.ajaxSettings.xhr();
                if(myXhr.upload){ //检查upload属性是否存在
                    //绑定progress事件的回调函数
                    myXhr.upload.addEventListener('progress',function (e) {
                        progressHandlingFunction(e, progressId);

                    }, false);
                }
                myXhr.addEventListener("load", uploadComplete, false);
                myXhr.addEventListener("error", uploadFailed, false);
                myXhr.addEventListener("abort", uploadCanceled, false);
                return myXhr; //xhr对象返回给jQuery使用
            },
            success: function(result){
                unsubmit(false);
                console.log(isChange);
                if(isChange){
                    setInterval(function (){getprogress(result.data.wid);},2000);
                }
            },
            contentType: false, //必须false才会自动加上正确的Content-Type
            processData: false  //必须false才会避开jQuery对 formdata 的默认处理
        });
        //上传进度回调函数：
        function progressHandlingFunction(e, id) {
            if (e.lengthComputable) {
                console.log(e);
                var percent = e.loaded/e.total*100;
                percent =percent.toFixed(2) + "%";
                // $('#body-row > div .progress-bar').css('width',percent);
                id = "#" + id;
                console.log($(id));
                $(id).css('width',percent);
            }
        }
        function uploadComplete(evt) {
            /* 当服务器响应后，这个事件就会被触发 */
            console.log(evt.target.responseText);
        }

        function uploadFailed(evt) {
            alert("上传文件发生了错误尝试");
        }

        function uploadCanceled(evt) {
            alert("上传被用户取消或者浏览器断开连接");
        }


    }else{
        alert('请先登录!');
        $("#dropbox").val('');
        showLogin(0);
    }
    return false;
}

//拿取源语言
function getsrcLen(){
    $.ajax({
        url:URL_ip+"/getSrcLan",
        type:"post",
        error: function(error) {
            //失败原因
        },
        success:function(data){
            $('#src-button').html('');
            $('#src-more').html('');
            $('#src-button1').html('');
            $('#src-more1').html('');
            if(data.code==0){
                var html='<div class="lanBox"></div><div class="lanBox"></div><div class="lanBox"></div><div class="triangle" style="left:100px;top:-10px;"><div class="triangle_inner"></div></div>';
                $('#src-more').append(html);
                $('#src-more1').append(html);
                for(var i=0;i<data.data.length;i++){
                    console.log(data.data[i].language);
                    if(data.data.length<1){
                        $('#src-button').append('<button type="button" class="btn btn-success" onclick="changedstLan(this,true)"  id="'+data.data[i].languageId+'" index="'+i+'"  style="outline: none;">'+data.data[i].language+'</button>');
                        $('#src-more').css('background','transparent');
                        $('#src-button1').append('<button type="button" class="btn btn-success" onclick="changedstLan1(this,true)"  id="'+data.data[i].languageId+'" index="'+i+'"  style="outline: none;">'+data.data[i].language+'</button>');
                        $('#src-more1').css('background','transparent');
                    }else{
                        if(i<1){
                            $('#src-button1').append('<button type="button" class="btn btn-default buttonimg" data-target="#src-more"  id="select1" index="'+i+'" style="outline: none;margin-right: 16px" >'+'<p style="color:rgb(176 176 176);margin-right: 22px" >'+"请选择"+'</p>'+'</button>');
                            $('#src-button').append('<button type="button" class="btn btn-default buttonimg" data-target="#src-more"  id="select1" index="'+i+'" style="outline: none;margin-right: 16px" >'+'<p style="color:rgb(176 176 176);margin-right: 22px" >'+"请选择"+'</p>'+'</button>');
                        }
                        $('#src-more > div').eq(i%3).append('<a onclick="changedstLan(this,true)" id="'+data.data[i].languageId+'" index="'+i+'" name="'+data.data[i].languageId+'" >'+data.data[i].language+'</a><br>');
                        $('#src-more1 > div').eq(i%3).append('<a onclick="changedstLan1(this,true)" id="'+data.data[i].languageId+'" index="'+i+'" name="'+data.data[i].languageId+'" >'+data.data[i].language+'</a><br>');

                    }
                }
                var that=$('#src-button > button').eq(0);
                changedstLan(that);
                var that1=$('#src-button1 > button').eq(0);
                changedstLan(that1);

            }
        }
    })
}

//拿目的语言
function changedstLan1(that,key){
    //$(element).is('input')

    $('#dest-more1').find('a').css("cursor",'pointer');
    $('#dest-more1').find('a').css("color","#000");
    $('#dest-more1').find('a').click(function(){changeArea(this,10)});
    var srcID=$(that).attr('id');//源语言
    var a=$(that).attr("name");
    $('#dest-more1').find('a[name='+a+']').css("cursor","not-allowed");
    $('#dest-more1').find('a[name='+a+']').css("color","#e5e5e5");
    $('#dest-more1').find('a[name='+a+']').attr("disabled",true);
    $('#dest-more1').find('a[name='+a+']').removeAttr("onclick");
    $('#dest-more1').find('a[name='+a+']').unbind();


    /* console.log(a);*/

    if($(that).closest('div').attr('id')=='src-button'){
        $("#src-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#src-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#src-more1 > a").eq(0).attr('index',num);
            $("#src-button1 > button").eq(0).html(val);
            $("#src-button1 > button").eq(0).attr('id',srcID);
            $("#src-button1 > button").eq(0).css('border','1px solid #e5e5e5');
            $("#src-button1 > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#src-more1  a").eq(0).html();
                var id=$("#src-more1  a").eq(0).attr('id');
                $("#src-button1 button").eq(0).html(val);
                $("#src-button1 button").eq(0).attr('id',id);
            }
            $("#src-button1 > button").eq(num).css('border','1px solid #e5e5e5');
            $("#src-button1 > button").eq(num).addClass('border_select');
        }
        $("#src-more1").hide();
    }
    if(!key){
        getdstLan(that,key);
    }
}
function changedstLan1(that,key){
    //$(element).is('input')
    $('#dest-more1').find('a').css("cursor",'pointer');
    $('#dest-more1').find('a').css("color","#000");
    $('#dest-more1').find('a').click(function(){changeArea1(this,10)});
    $('#dest-more').find('a').css("cursor",'pointer');
    $('#dest-more').find('a').css("color","#000");
    $('#dest-more').find('a').click(function(){changeArea(this,10)});
    var srcID=$(that).attr('id');//源语言
    var a=$(that).attr("name");
    $('#dest-more1').find('a[name='+a+']').css("cursor","not-allowed");
    $('#dest-more1').find('a[name='+a+']').css("color","#e5e5e5");
    $('#dest-more1').find('a[name='+a+']').attr("disabled",true);
    $('#dest-more1').find('a[name='+a+']').removeAttr("onclick");
    $('#dest-more1').find('a[name='+a+']').unbind();
    $('#dest-more').find('a[name='+a+']').css("cursor","not-allowed");
    $('#dest-more').find('a[name='+a+']').css("color","#e5e5e5");
    $('#dest-more').find('a[name='+a+']').attr("disabled",true);
    $('#dest-more').find('a[name='+a+']').removeAttr("onclick");
    $('#dest-more').find('a[name='+a+']').unbind();


    /* console.log(a);*/

    if($(that).closest('div').attr('id')=='src-button'){
        $("#src-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#src-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#src-more1 > a").eq(0).attr('index',num);
            $("#src-button1 > button").eq(0).html(val);
            $("#src-button1 > button").eq(0).attr('id',srcID);
            $("#src-button1 > button").eq(0).css('border','1px solid #e5e5e5');
            $("#src-button1 > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#src-more1  a").eq(0).html();
                var id=$("#src-more1  a").eq(0).attr('id');
                $("#src-button1 button").eq(0).html(val);
                $("#src-button1 button").eq(0).attr('id',id);
            }
            $("#src-button1 > button").eq(num).css('border','1px solid #e5e5e5');
            $("#src-button1 > button").eq(num).addClass('border_select');
        }
    }
    if($(that).closest('div').attr('id')=='src-button'){
        $("#src-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#src-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#src-more > a").eq(0).attr('index',num);
            $("#src-button > button").eq(0).html(val);
            $("#src-button > button").eq(0).attr('id',srcID);
            $("#src-button > button").eq(0).css('border','1px solid #e5e5e5');
            $("#src-button > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#src-more  a").eq(0).html();
                var id=$("#src-more  a").eq(0).attr('id');
                $("#src-button button").eq(0).html(val);
                $("#src-button button").eq(0).attr('id',id);
            }
            $("#src-button > button").eq(num).css('border','1px solid #e5e5e5');
            $("#src-button > button").eq(num).addClass('border_select');
        }
    }
    $("#src-more1").hide();
    if(!key){
        getdstLan(that,key);
    }
}
function changedstLan(that,key){
    //$(element).is('input')
    $('#dest-more').find('a').css("cursor",'pointer');
    $('#dest-more').find('a').css("color","#000");
    $('#dest-more').find('a').click(function(){changeArea(this,10)});
    var srcID=$(that).attr('id');//源语言
    var a=$(that).attr("name");
    $('#dest-more').find('a[name='+a+']').css("cursor","not-allowed");
    $('#dest-more').find('a[name='+a+']').css("color","#e5e5e5");
    $('#dest-more').find('a[name='+a+']').attr("disabled",true);
    $('#dest-more').find('a[name='+a+']').removeAttr("onclick");
    $('#dest-more').find('a[name='+a+']').unbind();


    /* console.log(a);*/

    if($(that).closest('div').attr('id')=='src-button'){
        $("#src-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#src-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#src-more > a").eq(0).attr('index',num);
            $("#src-button > button").eq(0).html(val);
            $("#src-button > button").eq(0).attr('id',srcID);
            $("#src-button > button").eq(0).css('border','1px solid #e5e5e5');
            $("#src-button > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#src-more  a").eq(0).html();
                var id=$("#src-more  a").eq(0).attr('id');
                $("#src-button button").eq(0).html(val);
                $("#src-button button").eq(0).attr('id',id);
            }
            $("#src-button > button").eq(num).css('border','1px solid #e5e5e5');
            $("#src-button > button").eq(num).addClass('border_select');
        }
    }
    if(!key){
        getdstLan(that,key);
    }
}
function getdstLan(that,key){
    var srcID=1;//源语言id
    var data={
        "languageId":2
    };
    $.ajax({
        url: URL_ip + "/getDestLan",
        type: "post",
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify(data),
        error: function(error) {
            //失败原因
        },
        success:function(data){
            console.log(data);
            $('#dst-button').html('');
            $('#dest-more').html('');
            $('#dst-button1').html('');
            $('#dest-more1').html('');
            if(data.code==0){
                var html='<div class="lanBox"></div><div class="lanBox"></div><div class="lanBox"></div><div class="triangle" style="left:100px;top:-10px;"><div class="triangle_inner"></div></div>';
                $('#dest-more').append(html);
                $('#dest-more1').append(html);
                for(var i=0;i<data.data.length;i++){
                    if(false){
                        $('#dst-button').append('<button type="button" class="btn btn-default" onclick="changeArea(this,'+srcID+','+key+')" id="'+data.data[i].languageId+'" index="'+i+'" style="outline: none;"">'+data.data[i].language+'</button>');
                        $('#dest-more').css('background','transparent');
                        $('#dst-button1').append('<button type="button" class="btn btn-default" onclick="changeArea1(this,'+srcID+','+key+')" id="'+data.data[i].languageId+'" index="'+i+'" style="outline: none;"">'+data.data[i].language+'</button>');
                        $('#dest-more1').css('background','transparent');
                    }else{
                        if(i<1){
                            $('#dst-button1').append('<button type="button" class="btn btn-default buttonimg"  id="select2" index="'+i+'" dataId="'+data.data[i].languageId+'" style="outline: none;" >'+data.data[i].language+'</button>');
                            $('#dst-button').append('<button type="button" class="btn btn-default buttonimg"  id="select2" index="'+i+'" dataId="'+data.data[i].languageId+'" style="outline: none;" >'+data.data[i].language+'</button>');
                        }
                        $('#dest-more1> div').eq(i%3).append('<a onclick="changeArea1(this,'+srcID+','+key+')" id="'+data.data[i].languageId+'" index="'+i+'" name="'+data.data[i].languageId+'">'+data.data[i].language+'</a><br>');
                        $('#dest-more> div').eq(i%3).append('<a onclick="changeArea(this,'+srcID+','+key+')" id="'+data.data[i].languageId+'" index="'+i+'" name="'+data.data[i].languageId+'">'+data.data[i].language+'</a><br>');
                    }
                }
                var that=$('#dst-button > button').eq(0);
                changeArea(that,srcID,key);
                var that1=$('#dst-button1 > button').eq(0);
                changeArea(that1,srcID,key);
            }
        }
    })
}
//获取领域信息
function changeArea1(that,srcID,key){
    var destID=$(that).attr('id');//目的语言id
    var a=$(that).attr("name");
    console.log("guan");
    $('#src-more1').find('a').css("cursor",'pointer');
    $('#src-more1').find('a').css("color","#000");
    $('#src-more1').find('a').click(function(){changedstLan1(this,true)});
    $('#src-more1').find('a[name='+a+']').css("cursor","not-allowed");
    $('#src-more1').find('a[name='+a+']').css("color","#e5e5e5");
    $('#src-more1').find('a[name='+a+']').attr("disabled",true);
    $('#src-more1').find('a[name='+a+']').removeAttr("onclick");
    $('#src-more1').find('a[name='+a+']').unbind();
    $('#src-more').find('a').css("cursor",'pointer');
    $('#src-more').find('a').css("color","#000");
    $('#src-more').find('a').click(function(){changedstLan(this,true)});
    $('#src-more').find('a[name='+a+']').css("cursor","not-allowed");
    $('#src-more').find('a[name='+a+']').css("color","#e5e5e5");
    $('#src-more').find('a[name='+a+']').attr("disabled",true);
    $('#src-more').find('a[name='+a+']').removeAttr("onclick");
    $('#src-more').find('a[name='+a+']').unbind();
    if($(that).closest('div').attr('id')=='dst-button'){
        $("#dst-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#dst-button1 > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#dest-more1 > a").eq(0).attr('index',num);
            $("#dst-button1 > button").eq(0).html(val);
            $("#dst-button1 > button").eq(0).attr('id',destID);
            $("#dst-button > button").eq(0).attr('dataid',destID);
            $("#dst-button1 > button").eq(0).attr('dataid',destID);
            $("#dst-button1 > button").eq(0).css('border','1px solid #e5e5e5');
            $("#dst-button1 > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#dest-more1  a").eq(0).html();
                var id=$("#dest-more1  a").eq(0).attr('id');
                $("#dst-button1 > button").eq(0).html(val);
                $("#dst-button1 > button").eq(0).attr('id',id);
            }
            $("#dst-button1 > button").eq(num).css('border','1px solid #e5e5e5');
            $("#dst-button1 > button").eq(num).addClass('border_select');
        }
    }
    if($(that).closest('div').attr('id')=='dst-button'){
        $("#dst-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#dst-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#dest-more > a").eq(0).attr('index',num);
            $("#dst-button > button").eq(0).html(val);
            $("#dst-button > button").eq(0).attr('id',destID);
            $("#dst-button > button").eq(0).css('border','1px solid #e5e5e5');
            $("#dst-button > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#dest-more  a").eq(0).html();
                var id=$("#dest-more  a").eq(0).attr('id');
                $("#dst-button > button").eq(0).html(val);
                $("#dst-button > button").eq(0).attr('id',id);
            }
            $("#dst-button > button").eq(num).css('border','1px solid #e5e5e5');
            $("#dst-button > button").eq(num).addClass('border_select');
        }
    }
    $("#dest-more1").hide();
    if(!key){
        getArea(that,srcID);
    }

}
function changeArea(that,srcID,key){
    var destID=$(that).attr('id');//目的语言id
    var a=$(that).attr("name");
    $('#src-more').find('a').css("cursor",'pointer');
    $('#src-more').find('a').css("color","#000");
    $('#src-more').find('a').click(function(){changedstLan(this,true)});
    $('#src-more').find('a[name='+a+']').css("cursor","not-allowed");
    $('#src-more').find('a[name='+a+']').css("color","#e5e5e5");
    $('#src-more').find('a[name='+a+']').attr("disabled",true);
    $('#src-more').find('a[name='+a+']').removeAttr("onclick");
    $('#src-more').find('a[name='+a+']').unbind();
    if($(that).closest('div').attr('id')=='dst-button'){
        $("#dst-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        $(that).css('border','1px solid #e5e5e5');
        $(that).addClass('border_select');
    }
    if($(that).closest('div').hasClass('lanBox')){
        $("#dst-button > button").each(function(){
            $(this).css('border','1px solid #ccc');
            $(this).removeClass('border_select');
        })
        if($(that).attr('index')>=0){
            var val=$(that).html();
            var num=$(that).attr('index');
            $("#dest-more > a").eq(0).attr('index',num);
            $("#dst-button > button").eq(0).html(val);
            $("#dst-button > button").eq(0).attr('id',destID);
            $("#dst-button > button").eq(0).attr('dataid',destID);
            $("#dst-button1 > button").eq(0).attr('dataid',destID);
            $("#dst-button > button").eq(0).css('border','1px solid #e5e5e5');
            $("#dst-button > button").eq(0).addClass('border_select');
        }else{
            var num=$(that).attr('index');
            if(num==0){
                var val=$("#dest-more  a").eq(0).html();
                var id=$("#dest-more  a").eq(0).attr('id');
                $("#dst-button > button").eq(0).html(val);
                $("#dst-button > button").eq(0).attr('id',id);
            }
            $("#dst-button > button").eq(num).css('border','1px solid #e5e5e5');
            $("#dst-button > button").eq(num).addClass('border_select');
        }
    }
    if(!key){
        getArea(that,srcID);
    }

}
function getArea(that,srcID){
    var destID=1;//目的语言id
    var dataID={
        // 'userId':Number(sessionStorage['userId']),
        'srcLanguageId':1,
        'destLanguageId':2
    };
    $.ajax({
        url:URL_ip+"/getAreaInfo",
        type:"post",
        contentType: 'application/json',
        dataType: 'json',
        data:JSON.stringify(dataID),
        error: function(error) {
            //失败原因
        },
        success:function(data){
            $("#language-box").fadeIn(500);
            $('#area-select').html('');
            for(var i=0;i<data.data.length;i++){
                var dataIDs={
                    'srcLanId':srcID,
                    'destLanId':destID
                };
                dataIDs.areaId=data.data[i].aId;
                dataIDs.userId=sessionStorage['userId'];
                dataIDs=JSON.stringify(dataIDs);
                /*$("#dataIDs-box").val(dataIDs);*/
                $('#area-select').append('<li  id="'+data.data[i].aId+'" style="text-align: center">'+data.data[i].area+'</li>');
                $(".select_box input").attr("value",data.data[0].area);
                $(".select_box input").attr("id",data.data[0].aId);

            }
            $('#area-select').append('<div class="triangle" style="left:69px;top:-10px;"><div class="triangle_inner" style="top: -3px;"></div></div>')
        }
    })
}



//提交翻译文档
$(".checkbox1").click(function () {
    $("#Orderbtn").attr("data-target","null");
    if($('#select1').children().html()!=='请选择'&&$('#select2').children().html()!=='请选择'&&$("#body-row").html()!==""){
        $("#Orderbtn").removeClass();
        $("#Orderbtn").addClass('agreebutton');}
});
$("#Orderbtn").click(function(){
    var iskey=true;
    var length = dropbox.files.length;
    if($('#select1').children().html()=='请选择'||$('#select2').children().html()=='请选择'||$("#body-row").html()==""||$("#size").html()=="计算中..."){
        iskey=false;
    }
    var data={
        "srcLanId":$('#src-button > .border_select').attr('id'),
        "destLanId":$('#dst-button > .border_select').attr('dataId'),
        "areaId":$(".select_box input").attr('id'),
        "userId":sessionStorage['userId']
    }
    if(iskey){
        $.ajax({
            url:URL_ip+"/submitOrder",
            type:"post",
            contentType: 'application/json',
            dataType: 'json',
            data:JSON.stringify(data),
            beforeSend: function(request) {
                request.setRequestHeader("X-OAUTH-TOKEN", sessionStorage['token']);
            },
            error: function(XMLHttpRequest) {
                //失败原因
                timeOut_error(XMLHttpRequest);
            },
            success:function(data){
                //页面跳转到支付页面并且把最新的待提交列表信息存起来(怎么存)跨页面拿取显示在订单页
                window.location.href=''+'MyFile.html?true';
                sessionStorage['orderId'] = data.orderId;
            }
        })
    }
})
function getprogress(index){
    var id=index.toString();
    var data={
        "orderCacheId":id
    };
    $.ajax({
        url:URL_ip+"/getConvertState",
        type:"post",
        contentType: 'application/json',
        data:JSON.stringify(data),
        dataType: 'json',
        beforeSend: function(request) {
            request.setRequestHeader("X-OAUTH-TOKEN", sessionStorage['token']);
        },
        error: function(error) {
            //失败原因
        },
        success:function(data){
            console.log(JSON.stringify(data));
            if(data.data[0].isComplete == true) {
                console.log("文档转换成功");
                unsubmit(false);

            }
        }
    });
}
//初始化待提交文档信息
function unsubmit(type){
    var data={
        "userId":sessionStorage['userId']
    };
    $.ajax({
        url:URL_ip+"/searchWait",
        type:"post",
        contentType: 'application/json',
        dataType: 'json',
        data:JSON.stringify(data),
        beforeSend: function(request) {
            request.setRequestHeader("X-OAUTH-TOKEN", sessionStorage['token']);
            $("#file-loading").css('display','block');
        },
        error: function(error) {
            //失败原因
            $("#file-loading").css('display','none');
            timeOut_error(XMLHttpRequest);
        },
        success:function(data){
            console.log(JSON.stringify(data));

            $("#file-loading").remove();
            $('#body-row').html('');
            var price=data.price.toFixed(2);
            $('#total-amount').html(price);
            if(data.data.length!==0){
                $("#Orderbtn").removeAttr('disabled');
                $("#Orderbtn").removeClass('btn-default');

            }
            if(data.data.length==0){
                $("#Orderbtn").attr('disabled',false);

                $(".wordnumber1").hide();
                $(".wordnumber").hide();
                $(".wordnumber2").hide();
                $(".wordnumber3").hide();
                $(".row-footer").height(40);
            }
            var size=0;
            var wordsize=0;
            for(var i=0;i<data.data.length;i++){
                var wid = data.data[i].wid;
                if(size!=='计算中...') {
                    size = size + data.data[i].letterNum;
                }
                var index=i+1;
                var letternum=0;
                if(data.data[i].letterNum==""){
                    if(type){
                        setInterval(function (){getprogress(wid);},3000);
                    }
                    letternum='计算中...';
                    $("#Orderbtn").attr('disabled',false);
                    size='计算中...';
                    $("#Orderbtn").attr("data-target","#change");
                }else{
                    letternum=data.data[i].letterNum;
                }
                var index2= data.data[i].filename.lastIndexOf(".");
                /*    console.log(index2);*/
                var a=data.data[i].filename.substring(index2);
                /*         console.log(data.data[i].filename);*/
                /* console.log(a);*/
                a = false;
                if(a){
                    wordsize++;
                    $(".wordnumber1").show();
                    $(".wordnumber").show();
                    $(".wordnumber2").show();
                    $(".wordnumber3").show();
                    $(".row-footer").height(120);
                }
                // var direction = $('#src-button1 > .border_select').text() + '/' + $('#dst-button1 > #select2').text();
                var direction = data.data[i].srcLang + '/' + data.data[i].tgtLang;
                var html='<div class="content-file-row">'+
                    '<div class="col-md-1 col-xs-2">'+index+'</div>'+
                    '<div class="col-md-4 col-xs-4" title="'+data.data[i].filename+'" style="width: 25%">'+data.data[i].filename+'</div>'+
                    ' <div class="col-md-2 col-xs-2" style="width: 10%">'+letternum+'</div>'+
                    '<div class="col-md-3 col-xs-3"  style="width: 20%">'+
                    '<div ">'+'<p>'+'100%'+'</p>'+
                    '</div>'+
                    '</div>'+
                    '<div class="col-md-3 col-xs-3" style="width: 20%">'+direction+'</div>'
                    +
                    '<div class="col-md-2 col-xs-1"><a class="delete-btn" aria-hidden="true" onclick="deleteinfo(this)" id="'+data.data[i].wid+'" style="color:black;cursor:pointer; ">删除文件</a><img src="img/loading.gif" style="width:16px;height:16px;margin-top:8px;display:none"/></div>'+
                    '</div>';
                $('#body-row').append(html);
            }
            $("#titlenum").html(wordsize);
            $("#wordchange").html(wordsize*2);
            var x=parseFloat(price,10);
            /*console.log(x);*/
            var num=wordsize*2+x;
            if(data.data.length !== 0){
                searchInfo.srcLang = data.data[0].srcLang;
                searchInfo.srcLangId = data.data[0].srcLangId;
                searchInfo.tgtLang = data.data[0].tgtLang;
                searchInfo.tgtLangId = data.data[0].tgtLangId;
                $("#src-button1 > button").eq(0).html(searchInfo.srcLang);
                $("#src-button1 > button").eq(0).attr('id',searchInfo.srcLangId);
                $("#src-button > button").eq(0).html(searchInfo.srcLang);
                $("#src-button > button").eq(0).attr('id',searchInfo.srcLangId);
            }
            $("#wordnum").html(num.toFixed(2));
            $("#size").html(size);

        }
    })
}
//删除文档
function deleteinfo(that){
    var data={
        "wid":that.id
    };
    $.ajax({
        url:URL_ip+"/deleteWait",
        type:"post",
        contentType: 'application/json',
        dataType: 'json',
        data:JSON.stringify(data),
        beforeSend: function(request) {
            request.setRequestHeader("X-OAUTH-TOKEN", sessionStorage['token']);
            $(that).css('display','none');
            $(that).next().css('display','inline');
        },
        error: function(error) {
            //失败原因
            timeOut_error(XMLHttpRequest);
        },
        success:function(data){
            unsubmit(false);
        }
    })
}
function srcshow(){
    $('#src-more').show();
}


$(document).ready(function(){
    $(".select_box").click(function(event){
        if( $('.select_ul').css('display')=='block'){
            $('.select_ul').hide();
        }else if($('.select_ul').css('display')=='none'){
            $('.select_ul').show();
        }

        if($("#dest-more").css('display')=='block'){
            $('#dest-more').hide();
        }
        if($("#src-more").css('display')=='block'){
            $('#src-more').hide();
        }
        event.stopPropagation();
        var thisinput=$(".select_box input");
        var thisul=$(".select_box input").parent().find("ul");

        if(thisul.height()>200){thisul.css({height:"200"+"px","overflow-y":"scroll" })};


        thisul.find("li").click(function(){thisinput.val($(this).text());thisinput.attr("id",$(this).attr("id"));});


    })
    $("#submit").click(function(){
        var endinfo="";
        $(".select_box input:text").each(function(i){
            endinfo=endinfo+(i+1)+":"+$(this).val()+";\n";
        })
        alert(endinfo);
    })
});
